require("sgenum")
local tabletool = require "tabletool"
local table_sort = table.sort
local sg = {}

-- 52张牌编码
local cardskey = {
    1,2,3,4,        -- A
    5,6,7,8,        -- 2
    9,10,11,12,     -- 3
    13,14,15,16,    -- 4
    17,18,19,20,    -- 5
    21,22,23,24,    -- 6
    25,26,27,28,    -- 7
    29,30,31,32,    -- 8
    33,34,35,36,    -- 9
    37,38,39,40,    -- 10
    41,42,43,44,    -- J
    45,46,47,48,    -- Q
    49,50,51,52,    -- K
}
-- 编码对应的牌
local cardsvalue = {
    1,1,1,1,
    2,2,2,2,
    3,3,3,3,
    4,4,4,4,
    5,5,5,5,
    6,6,6,6,
    7,7,7,7,
    8,8,8,8,
    9,9,9,9,
    10,10,10,10,
    11,11,11,11,    -- J
    12,12,12,12,    -- Q
    13,13,13,13,    -- K
}
-- 编码对应的花色
-- 1,2,3,4 方片,草花,红桃,黑桃
local cardscolor = {
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
    1,2,3,4,
}

-- 洗牌
sg.shuffle = function(initcards)
    initcards = tabletool.deepcopy(cardskey)
    -- 对牌池中的牌洗牌
    local r = 1
    for i = 1, #initcards do
        r = math.random(#initcards + 1 - i)
        initcards[r], initcards[#initcards + 1 - i] = initcards[#initcards + 1 - i], initcards[r]
    end
end

-- 给牌从大到小排序
sg.sort = function(cards)
    table_sort(cards, function(l, r)
        return l < r
    end)
end

-- 获取牌的类型
sg.getcardtype = function(cards)
    local cv1, cv2, cv3 = cardsvalue[cards[1]], cardsvalue[cards[2]], cardsvalue[cards[3]]
    if cv1 == cv2 and cv1 == cv3 then
        if cv1 == 3 then
            return ESGCardsType.CARDS_TYPE_BAOSAN   -- 爆三
        else
            return ESGCardsType.CARDS_TYPE_SANTIAO  -- 三条
        end
    elseif cv1 > 10 and cv2 > 10 and cv3 > 10 then
        return ESGCardsType.CARDS_TYPE_SANGONG      -- 三公
    else
        local totalval = sg.getcardvalue(cards)
        if totalval == 9 then
            return ESGCardsType.CARDS_TYPE_JIUDIAN  -- 九点
        elseif cv1 == 8 then
            return ESGCardsType.CARDS_TYPE_BADIAN   -- 八点
        end
    end
    return ESGCardsType.CARDS_TYPE_SANPAI
end

-- 获取牌的余值
sg.getcardvalue = function(cards)
    local totalval = 0
    for _, v in ipairs(cards) do
        if v < 10 then
            totalval = totalval + v
        end
    end
    return totalval % 10
end

-- 比牌
    -- 注意：这里的牌默认是从大到小排好序的
sg.compare = function(lcards, rcards)
    local lt = sg.getcardtype(lcards)
    local rt = sg.getcardtype(rcards)
    if lt == ESGCardsType.CARDS_TYPE_BAOSAN then
        return true
    elseif rt == ESGCardsType.CARDS_TYPE_BAOSAN then
        return false
    end
    if lt == rt then
        if lt == ESGCardsType.CARDS_TYPE_SANTIAO then
            return cardsvalue[lcards[1]] > cardsvalue[rcards[1]]
        elseif lt == ESGCardsType.CARDS_TYPE_SANGONG then
            if cardsvalue[lcards[1]] == cardsvalue[rcards[1]] then
                return cardscolor[lcards[1]] > cardscolor[rcards[1]]
            else
                return cardsvalue[lcards[1]] > cardsvalue[rcards[1]]
            end
        else
            local lcv = sg.getcardvalue(lcards)
            local rcv = sg.getcardvalue(rcards)
            if lcv == rcv then
                if cardsvalue[lcards[1]] == cardsvalue[rcards[1]] then
                    return cardscolor[lcards[1]] > cardscolor[rcards[1]]
                else
                    return cardsvalue[lcards[1]] > cardsvalue[rcards[1]]
                end
            else
                return lcv > rcv
            end
        end
    else
        return lt > rt
    end
    return true
end

sg.gettimes = function(cards)
    local times = 1
    -- local cardtype = sg.getcardtype(cards)
	-- local cardvalue = sg.getcardvalue(cards)
    
	-- if cardtype == ESGCardsType.CARD_TYPE_PAIR then
	-- 	if cardsvalue[cards[1]] == 10 then
	-- 		times = 5
	-- 	else
	-- 		times = 4
	-- 	end
	-- elseif cardtype == ESGCardsType.CARD_TYPE_28 then
	-- 	times = 3
	-- else
	-- 	if cardvalue > 7 then
	-- 		times = 2
	-- 	end
    -- end
    return times
end

return sg